package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.entity.Colleges;
import com.example.demo.mapper.CollegesMapper;
import com.example.demo.mapper.MajorsMapper;
import com.example.demo.entity.Majors;
import com.example.demo.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private MajorsMapper majorsMapper;

    @Autowired
    private CollegesMapper collegesMapper;

    @RequestMapping("/selectAll")
    public String selectAll(){
        List<Majors> majors = majorsMapper.selectAll();
        return JSONObject.toJSONString(Message.success(majors));
    }

    @RequestMapping("/selectAllVersion")
    public String selectAllVersion(){
        List<Colleges> majors = collegesMapper.selectAllVersion();
        return JSONObject.toJSONString(Message.success(majors));
    }

    @RequestMapping("/selectByCollege/{college}")
    public String selectByCollege(@PathVariable String college){
        if ("all".equals(college)) college = null;
        List<Colleges> majors = collegesMapper.selectByCollege(college);
        return JSONObject.toJSONString(Message.success(majors));
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request){
        Majors majors=new Majors();
        String college = request.getParameter("college");
        String major = request.getParameter("major");
        if(college==null||"".equals(college)) return JSONObject.toJSONString(Message.failure("college can not null"));
        if(major==null||"".equals(major)) {
            majors.setStatus(0);
        }else  majors.setStatus(1);
        Majors majorss = majorsMapper.selectByCollege(college);
        if(majorss!=null) return JSONObject.toJSONString(Message.failure("college is exist,please update operation"));
        majors.setCollege(college);
        majors.setMajor(major);
        majors.setCreatetime(LocalDate.now());
        int add = majorsMapper.add(majors);
        if(add>0) return JSONObject.toJSONString(Message.success("success add"));
        else return JSONObject.toJSONString(Message.failure("add failure"));
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request){
        int flag = Integer.valueOf(request.getParameter("flag"));
        Majors majors = majorsMapper.selectByFlag(flag);
        if(majors.getStatus()==1) return JSONObject.toJSONString(Message.failure("This is in use,can not to delete"));
        int delete = majorsMapper.delete(flag);
        if(delete>0) return JSONObject.toJSONString(Message.success("success delete"));
        else return JSONObject.toJSONString(Message.failure("delete failure"));
    }

    @RequestMapping("/update")
    public String update(HttpServletRequest request){
        int flag = Integer.valueOf(request.getParameter("flag"));
        String college=request.getParameter("college");
        String major=request.getParameter("major");
        String status=request.getParameter("status");
        Majors majors=new Majors();
        majors.setFlag(flag);
        if(college!=null&&!"".equals(college)) majors.setCollege(college);
        if(major!=null&&!"".equals(major)) majors.setMajor(major);
        if(status!=null&&!"".equals(status)) {
            int statu="1".equals(status)?0:1;
            majors.setStatus(statu);
        }
        majors.setCreatetime(LocalDate.now());
        int update = majorsMapper.update(majors);
        if(update>0) return JSONObject.toJSONString(Message.success("success update"));
        else return JSONObject.toJSONString(Message.failure("update failure"));
    }
}
